<!DOCTYPE html>
<!--suppress ALL -->
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>bind(绑定)</title>
</head>

<body>
<h4>
    Creates a function that invokes fn with a given context, optionally adding any additional supplied parameters to the
    beginning of the arguments.
    Return a function that uses Function.apply() to apply the given context to fn.
    Use Array.concat() to prepend any additional supplied parameters to the arguments.
</h4>
<h4>
    ...args 利用对象扩展符来标明这是个数组
    ...arguments 在 函数fn里面,所以...arguments的扩展就是传入函数fn参数的扩展,这里就是"hi", "!"
    context 就是传入第三方对象,供apply使用,而apply就是给对象传入新的this,这里就是给函数fn传入
    apply 需要全新的数组参数,所以这里就使用了args 来拼接了全新的数组
</h4>
<script type="text/javascript">
    const bind = (fn, context, ...args) =>
        function () {
            return fn.apply(context, args.concat(...arguments));
        }

    function greet(greeting, punctuation) {
        return greeting + " " + this.user + punctuation
    }

    const freddy = {user: "fred"}
    const freddyBound = bind(greet, freddy);
    console.log(freddyBound("hi", "!")) // hi fred!
</script>
</body>

</html>